package com.huatai.bi.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huatai.bi.dto.DataSynPrarmDto;
import com.huatai.bi.dto.DataTableSynPrarmDto;
import com.huatai.bi.entity.TableColumnEntity;
import com.huatai.bi.service.*;
import com.huatai.bi.vo.TablesVo;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @program: itc-airport-cloud
 * @description: 数据同步
 * @author: songxiude
 * @create: 2024-07-02 15:06
 **/
@RestController
@Api(value = "数据同步", tags = "数据同步")
@RequestMapping("/dataSyn")
public class DataSynController {

    @Autowired
    private TableService tableService;

    @Autowired
    private TableColumnService tableColumnService;

    @Autowired
    private DataSynrecordService dataSynrecordService;


    @PostMapping("/getTables")
    @ApiOperation(value = "获取表生成信息")
    public R getTables(@RequestBody DataTableSynPrarmDto synPrarmDto) {
        List<TablesVo> tables = tableService.getTables(synPrarmDto);
        return R.data(tables);
    }

    @GetMapping("/getTableColumns")
    @ApiOperation("获取表字段信息")
    public R getTableColumns(@RequestParam("table_id") Long table_id) {
        List<TableColumnEntity> list = tableColumnService.list(Wrappers.<TableColumnEntity>lambdaQuery()
                .eq(TableColumnEntity::getTableId, table_id)
                .orderByAsc(TableColumnEntity::getColumnPosition)
        );
        List<String> tablecolumn = Optional.ofNullable(list).map(tableColumnEntities -> {
            List<String> columns = new ArrayList<>();
            tableColumnEntities.forEach(column -> {
                columns.add(column.getColumnName());
            });
            return columns;
        }).orElse(Collections.emptyList());
        return R.data(tablecolumn);
    }

    @PostMapping("/generateDataSynTask")
    @ApiOperation(value = "生成任务调度并执行数据同步")
    public R generateDataSynTask(@RequestBody DataSynPrarmDto prarmDto) {
        List<String> strings = dataSynrecordService.generateDataSynTask(prarmDto);
        return R.data(strings, "任务已开始执行，请到数据集管理刷新查看执行结果");
    }

    @PostMapping("/execOneTableSyn")
    @ApiOperation(value = "单表执行数据同步")
    public R execOneTableSyn(@ApiParam(value = "数据集id", name = "dir_id", required = true) @RequestParam("dir_id") Long dirId,
                             @ApiParam(value = "表id", name = "tabel_id", required = true) @RequestParam("tabel_id") Long tableId) {
        dataSynrecordService.execOneTableSyn(dirId, tableId);
        return R.data(null, "任务已开始执行，请到数据集管理刷新查看执行结果");
    }

    @PostMapping("/editDataSynrecord")
    @ApiOperation(value = "数据同步调度修改")
    public R editDataSynrecord(@ApiParam(value = "id", name = "调度id", required = true) @RequestParam("id") Long id,
                               @ApiParam(value = "cron", name = "cron表达式如：*/5 * * * *", required = true) @RequestParam("cron") String cron) {
        boolean synrecord = dataSynrecordService.editDataSynrecord(id, cron);
        if (!synrecord) {
            return R.fail("修改失败");
        }
        return R.data(null, "修改成功");
    }


}
